package edu.nf.ch03.service;

import edu.nf.ch03.entity.User;
import edu.nf.ch03.mapper.UserMapper;
import edu.nf.ch03.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * @author wangl
 * @date 2025/4/23
 */
public class UserService {

    public User getUserById(int id) {
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            return session.getMapper(UserMapper.class)
                    .getUserById(id);
        }
    }

    public List<User> listUsers() {
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            return session.getMapper(UserMapper.class)
                    .listUsers();
        }
    }

    public Map<String, Object> getUserByName(String name) {
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            return session.getMapper(UserMapper.class)
                    .getUserByName(name);
        }
    }

    public List<Map<String, Object>> listUsersMap() {
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            return session.getMapper(UserMapper.class)
                    .listUsersMap();
        }
    }
}